<?php
/**
 * Class that operate on table 'projects'. Database Sqlite.
 *
 * @author: Dragan Milchevski
 * @date: 2012-06-23 13:23
 */
class ProjectsSqliteDAO implements ProjectsDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return ProjectsSqlite 
	 */
	public function load($id){
		$sql = 'SELECT * FROM projects WHERE projectID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM projects';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM projects ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param project primary key
 	 */
	public function delete($projectID){
		$sql = 'DELETE FROM projects WHERE projectID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($projectID);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param ProjectsSqlite project
 	 */
	public function insert($project){
		$sql = 'INSERT INTO projects (userID, project) VALUES (?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($project->getUser());//Check if User object can be put, and call getUser()->getId();
		$sqlQuery->set($project->project);

		$id = $this->executeInsert($sqlQuery);	
		$project->setId($id);
		return $id;
	}
	

	/**
	 * Insert record to table projectsfiles
	 *
	 * @param ProjectsSqlite project
	 */
	public function insertProjectsfiles($project_id,$document_id){
		if(!isset($document_id) && !isset($project_id)) return null;
		$sql = 'INSERT OR IGNORE INTO projectsfiles (projectID,fileID) VALUES (?,?)';
		$sqlQuery = new SqlQuery($sql);
	
		$sqlQuery->setNumber($project_id);
		$sqlQuery->setNumber($document_id);
	
		$id = $this->executeInsert($sqlQuery);
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param ProjectsSqlite project
 	 */
	public function update($project){
		$sql = 'UPDATE projects SET userID = ?, project = ? WHERE projectID = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($project->getUser());
		$sqlQuery->set($project->getProject());

		$sqlQuery->setNumber($project->getId());
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM projects';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByUserID($value){
		$sql = 'SELECT DISTINCT projects.projectID AS projectID,project FROM projects
                            LEFT OUTER JOIN projectsusers ON projects.projectID=projectsusers.projectID
                            WHERE projects.userID=? OR projectsusers.userID=? ORDER BY project COLLATE NOCASE ASC';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByProject($value){
		$sql = 'SELECT * FROM projects WHERE project = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}
	
	public function queryByDocument($value){
		$sql = 'SELECT b.* FROM projectsfiles a, projects b WHERE a.projectID = b.projectID AND a.fileID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByUserID($value){
		$sql = 'DELETE FROM projects WHERE integer = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByProject($value){
		$sql = 'DELETE FROM projects WHERE text = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return ProjectsSqlite 
	 */
	protected function readRow($row){
		$project = new Project();
		
		$project->setId($row['projectID']);
		$project->setUser($row['userID']);
		$project->setProject($row['project']);

		return $project;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return ProjectsSqlite 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>